﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 护士工作站
{
    public partial class 床位调配 : Form
    {
        SqlConnection conn = new SqlConnection(DBHelper.connString);
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        public 床位调配()
        {
            InitializeComponent();
        }
        public void Fill()
        {
            string sql = "select Bed.BedNo,BedRoom,BedSubject,BedPrice,BedCase,BedType,BedGrade,BedCondition from Bed";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                da = new SqlDataAdapter(sql, conn);
                ds = new DataSet();
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
        }
    


      



        private void button_tc_Click(object sender, EventArgs e)
        {

            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_ZYNo.Text = "";
            tb_xm.Text = "";
            tb_ch.Text = "";
            tb_xb.Text = "";
            tb_ks.Text = "";
            cb_xfbch.Text = "";
        }

        private void button_cx_Click(object sender, EventArgs e)
        {
            string ZYNo = tb_ZYNo.Text.Trim();


            if (ZYNo == "")
            {
                MessageBox.Show("请输入要查询的内容！");
                tb_ZYNo.Focus();

                return;
            }
            string Hos = "select * from InHospital,Patient,Bed where InHospital.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand cmd = new SqlCommand(Hos, conn);


                if (ZYNo != "")
                {
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        dr.Read();



                        tb_ks.Text = dr["HosDepartment"].ToString();
                        tb_xb.Text = dr["PatSex"].ToString();
                        tb_xm.Text = dr["PatName"].ToString();
                        tb_ch.Text = dr["BedNo"].ToString();




                    }
                    dr.Close();
                }
            }
        }

        private void button_bc_Click(object sender, EventArgs e)
        {

            string hosNo = tb_ZYNo.Text.Trim();
           
            string bedNo = tb_ch.Text.Trim();
 
            string xbedNo = cb_xfbch.Text.Trim();
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string BNo = "select BedCondition from Bed where BedNo ='" + xbedNo + "' ";
                SqlCommand B = new SqlCommand(BNo, conn);
                conn.Open();
                string BB = Convert.ToString(B.ExecuteScalar());
              
                    string sql = string.Format("update Bed set BedCondition='已用'where Bed.BedNo='" + xbedNo + "'");
                    string ssql = string.Format("update Bed set BedCondition='未用'where Bed.BedNo='" + bedNo + "'");
                    string str = string.Format("update InHospital set BedNo='" + xbedNo + "'where InHospital.HosNo='" + hosNo + "'");
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    SqlCommand com = new SqlCommand(str, conn);
                    SqlCommand bed = new SqlCommand(ssql, conn);

                    int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                    int m = Convert.ToInt32(com.ExecuteNonQuery());
                    int z = Convert.ToInt32(bed.ExecuteNonQuery());
                    if (n == 1 && m == 1 && z == 1)
                    {
                        MessageBox.Show("更新成功！");
                    }
                    else
                    {
                        MessageBox.Show("更新失败！");
                    }
                    Fill();
                }
            }
       
        void Fillch()
        {


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string ssql = "select BedNo from Bed where BedSubject='" + this.tb_ks.Text + "'and BedCondition='未用'";
                SqlCommand cm = new SqlCommand(ssql, conn);
                DataSet ds = new DataSet();
                SqlDataAdapter da3 = new SqlDataAdapter(ssql, conn);
                da3.Fill(ds, "Bed");
                cb_xfbch.DisplayMember = "BedNo";
                cb_xfbch.DataSource = ds.Tables[0];
            }
        }

        private void 床位调配_Load(object sender, EventArgs e)
        {
            Fill();
           
        }

        private void tb_ks_TextChanged(object sender, EventArgs e)
        {
     Fillch();
        }

      

    }
}
